package com.wei.czz.framework.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.enums.common.messageFlow.MessageStatusEnum;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.vo.common.messageFlow.MessageFlowFormVo;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wei.czz.framework.common.dao.MessageFlowDao;
import com.wei.czz.framework.common.entity.MessageFlowEntity;
import com.wei.czz.framework.common.service.MessageFlowService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-05-12 17:12:18
 * className: MessageFlowServiceImpl
 * version: 1.0
 * description:
 */
@Service("messageFlowService")
@AllArgsConstructor
public class MessageFlowServiceImpl extends ServiceImpl<MessageFlowDao, MessageFlowEntity> implements MessageFlowService {

    private static final Logger log = LoggerFactory.getLogger(MessageFlowServiceImpl.class);

    @Override
    public void insert(MessageFlowEntity messageFlow) {
        int count = baseMapper.insert(messageFlow);
        log.info("插入消息发送记录完成。count={}", count);
    }

    @Override
    public Long saveOrEdit(MessageFlowEntity messageFlow) {
        Long id = messageFlow.getId();
        if (Objects.isNull(id)) {

            this.insert(messageFlow);
        } else {
            int count = baseMapper.updateById(messageFlow);
            log.info("修改消息发送记录完成。count={}", count);
        }
        return id;
    }

    @Override
    public PageDto<MessageFlowEntity> getPageList(MessageFlowFormVo formVo) {
        // mp分页对象
        Page<MessageFlowEntity> page = new Page<>(formVo.getPage(), formVo.getLimit());

        LambdaQueryWrapper<MessageFlowEntity> messageFlowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        messageFlowLambdaQueryWrapper.eq(StringUtils.isNotBlank(formVo.getExchange()), MessageFlowEntity::getExchange,
                        formVo.getExchange())
                .eq(StringUtils.isNotBlank(formVo.getQueue()), MessageFlowEntity::getQueue, formVo.getQueue())
                .eq(StringUtils.isNotBlank(formVo.getRoutingKey()), MessageFlowEntity::getRoutingKey,
                        formVo.getRoutingKey())
                .ge(Objects.nonNull(formVo.getStartCreateTime()), MessageFlowEntity::getCreateTime,
                        formVo.getStartCreateTime())
                .le(Objects.nonNull(formVo.getEndCreateTime()), MessageFlowEntity::getCreateTime,
                        formVo.getEndCreateTime());
        messageFlowLambdaQueryWrapper.orderByDesc(MessageFlowEntity::getId);
        // 分页查询
        baseMapper.selectPage(page, messageFlowLambdaQueryWrapper);
        log.info("分页查询消息发送记录完成。totalNum={} list.size={}", page.getTotal(), page.getRecords().size());
        return new PageDto<>(page);
    }

    @Override
    public List<MessageFlowEntity> getList(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            log.info("获取消息发送记录，传递主键列表为空");
            return Collections.emptyList();
        }
        LambdaQueryWrapper<MessageFlowEntity> messageFlowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        messageFlowLambdaQueryWrapper.in(MessageFlowEntity::getId, idList)
                .eq(MessageFlowEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<MessageFlowEntity> messageFlowList = baseMapper.selectList(messageFlowLambdaQueryWrapper);
        log.info("查询消息发送记录完成。size={}", messageFlowList.size());
        return messageFlowList;
    }

    @Transactional
    @Override
    public void batchUpdate(List<MessageFlowEntity> messageFlowList) {
        boolean bool = this.updateBatchById(messageFlowList);
        log.info("批量修改消息发送记录完成。bool={}", bool);
    }

}